"""
train.py这个文件中多了太多和训练不相关的代码，保存训练信息的功能应该作为工具类存在并被train.py调用，
应该将这个功能封装，并保存再utils中

设置Record2Json类 将需要处理的列表（数据） 和 保存列表到json文件的函数（方法） 绑定
"""

import time
import json


class Record2Json:

    def __init__(self, neural_network):
        self.Train = []
        self.Valid = []
        self.fileDir = "./tmp/"
        self.nowTimeStr = time.strftime("%Y_%m_%d_%Hh_%Mm_%Ss", time.localtime())
        self.trainRecord = self.fileDir + neural_network + '_train_' + self.nowTimeStr + '.json'
        self.validationRecord = self.fileDir + neural_network + '_valid_' + self.nowTimeStr + '.json'

    def recordTrain(self, info):
        self.Train.append(info)

    def recordValid(self, info):
        self.Valid.append(info)

    def write2json(self):
        with open(self.trainRecord, "w", encoding='utf-8') as f:
            json.dump(self.Train, f, indent=4)

        with open(self.validationRecord, "w", encoding='utf-8') as f:
            json.dump(self.Valid, f, indent=4)
